package com.ruoyi.system.mapper;

import java.math.BigDecimal;
import java.util.List;

import cn.hutool.core.date.DateTime;
import com.ruoyi.system.domain.User;
import com.ruoyi.system.dto.UserBagTopDTO;
import com.ruoyi.system.input.ListByType;
import com.ruoyi.system.input.SortByType;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

/**
 * 用户管理Mapper接口
 *
 * @author ruoyi
 * @date 2022-05-24
 */
public interface UserMapper
{
    /**
     * 查询用户管理
     *
     * @param id 用户管理主键
     * @return 用户管理
     */
    public User selectUserById(String id);

    /**
     * 查询用户管理列表
     *
     * @param user 用户管理
     * @return 用户管理集合
     */
    public List<User> selectUserList(User user);

    /**
     * 新增用户管理
     *
     * @param user 用户管理
     * @return 结果
     */
    public int insertUser(User user);

    /**
     * 修改用户管理
     *
     * @param user 用户管理
     * @return 结果
     */
    public int updateUser(User user);

    /**
     * 删除用户管理
     *
     * @param id 用户管理主键
     * @return 结果
     */
    public int deleteUserById(String id);

    /**
     * 批量删除用户管理
     *
     * @param ids 需要删除的数据主键集合
     * @return 结果
     */
    public int deleteUserByIds(String[] ids);

    /**
     * 范围查找
     * @param listByType
     * @return
     */
    List<User> selectUserTypeList(@Param("listByType") ListByType listByType);

    List<User> selectUserSortList(@Param("sortByType") SortByType sortByType);

    int updateUserMoneyOrJf(@Param("userId") String userId,@Param("change") Integer change,@Param("type") Integer type);

    int updateRecords(@Param("userId") String userId, @Param("mj") BigDecimal moneyOrJf,
                      @Param("change") Integer change, @Param("type") Integer type,@Param("creator") String creator);

    int selectUserCount();

    int selectTodayCount(@Param("start") String start,@Param("now") String now);

    Double selectUserCountMoney();

    BigDecimal selectMbProfit();

    BigDecimal selectJfProfit();

    BigDecimal selectUserCountMoneyV2(@Param("ifPrivate") Integer ifPrivate);

    Integer selectMbProfitV2(@Param("ifPrivate") Integer ifPrivate);

    Integer selectJfProfitV2(@Param("ifPrivate")Integer ifPrivate);

    Integer selectUserCountV2(@Param("ifPrivate")Integer ifPrivate);

    Integer selectTodayCountV2(@Param("begin") DateTime beginOfDay,@Param("end") DateTime endOfDay, @Param("ifPrivate") Integer ifPrivate);

    int updateUserStatus(@Param("openId") String openId, @Param("status") int i);

    @Select("SELECT \n" +
            "  u.id as userId, \n" +
            "  u.nickname as nickname, \n" +
            "  u.headimg as headimg, \n" +
            "  u.if_private as ifPrivate, \n" +
            "  COALESCE(total_orders.total_out_price, 0) AS totalAmount \n" +
            "FROM (\n" +
            "  SELECT \n" +
            "    user_id, \n" +
            "    SUM(out_price) AS total_out_price\n" +
            "  FROM (\n" +
            "    SELECT user_id, out_price FROM wx_order_list WHERE status = 1 \n" +
            "    UNION ALL\n" +
            "    SELECT user_id, recycling_price AS out_price FROM user_active_card_record WHERE status = 1 \n" +
            "    UNION ALL\n" +
            "    SELECT user_id, out_price FROM yfs_order_list WHERE status = 1 \n" +
            "  ) AS combined_orders\n" +
            "  GROUP BY user_id\n" +
            "  ORDER BY total_out_price DESC\n" +
            "  LIMIT 50\n" +
            ") AS total_orders\n" +
            "JOIN user u ON total_orders.user_id = u.id\n" +
            "ORDER BY totalAmount DESC;")
    List<UserBagTopDTO> selectUserBagTop();

}
